/**   
 *    
 * ��Ŀ��ƣ�a_yongan   
 * ����ƣ�deptController   
 * ��������   
 * �����ˣ�liubo   
 * ����ʱ�䣺2018��6��5�� ����11:45:59    
 * �޸�ʱ�䣺2018��6��5�� ����11:45:59   
 * �޸ı�ע��   
 * @version    
 *    
 */
package com.yongan.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.user.dto.UserExecution;
import com.yongan.user.dto.UserResult;
import com.yongan.user.entity.Dept;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.service.DeptService;
import com.yongan.user.util.StringToNumber;
import com.yongan.util.PageBean;
import com.yongan.util.Tree;

/**
 * @author liubo
 * 
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private DeptService deptService;

	@RequestMapping(value = "/deptlist", method = RequestMethod.GET)
	public String deptlist(Model model) {
		List<Dept> deptTree = deptService.getDeptListTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("menus", json);
		return "deptlist";
	}
	@RequestMapping(value = "/userlist", method = RequestMethod.GET)
	public String userdeptlist(Model model) {
		List<Dept> deptTree = deptService.getDeptListTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("menus", json);
		return "userlist";
	}
	@RequestMapping(value = "/{topId}/deptcontent", method = RequestMethod.GET)
	public String deptcontent(@PathVariable("topId") String topId, Model model,HttpServletRequest request) {
		List<Dept> deptlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<Dept> pb = new PageBean<Dept>();
		if (topId.equals("0")) {
			totalRecords = deptService.getDeptCount("", "");
		} else {
			totalRecords = deptService.getCountByTopId(topId);
		}
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			if (topId.equals("0")) {
				deptlist = deptService.getDeptList(start, end);
			} else {
				deptlist = deptService.getDeptListTreeByTopId(topId,start, end);
			}
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("deptlist", deptlist);
		return "deptcontent";
	}

	@RequestMapping(value = "/{deptId}/detail", method = RequestMethod.GET)
	public String deptdetail(@PathVariable("deptId") String deptId, Model model) {
		int ui = StringToNumber.stringToNumber(deptId);
		if (ui != 0) {
			Dept dept = deptService.getDeptById(deptId);
			model.addAttribute("deptDetail", dept);
		}
		return "deptdetail";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<UserExecution> deptupdate(Dept dept, Model model) {
		System.out.println(dept.toString() + "======");
		UserExecution rs = deptService.executeDept(dept, dept.getDeptId(),
				"123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<UserExecution>(true, rs);
		}
		return new UserResult<UserExecution>(false, "");
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String deptadd(Model model) {
		return "deptadd";
	}

	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<UserExecution> deptadddo(Dept dept, Model model) {
		System.out.println(dept.toString() + "======");
		UserExecution rs = deptService.executeDept(dept, "", "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<UserExecution>(true, rs);
		}
		return new UserResult<UserExecution>(false, "");
	}

	@RequestMapping(value = "/{deptId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<UserExecution> userDelete(
			@PathVariable("deptId") String deptId, Model model) {
		int ui = StringToNumber.stringToNumber(deptId);
		if (ui != 0) {
			int content = deptService.getCountByTopId(deptId);
			if(content>0){
				UserExecution ue = new UserExecution(deptId, UserStatEnum.DEPT_FAILED);
				return new UserResult<UserExecution>(false, ue);
			}
			UserExecution rsdelete = deptService.executeDept(null, deptId,
					"1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<UserExecution>(true, rsdelete);
			}
		}
		return new UserResult<UserExecution>(false, "");
	}

	
}
